<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Other\Channel as Channel;
use AuctionCore\Account\Back\Employee;
use AuctionCore\System\AuctionException;

class ChannelController extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->channel = new Channel();
        $this->emp_id = $this->user_info['emp_id'];
    }

    public function index() {
        $channel_list = $this->channel->get_channel_list('from_id,pid,from_name');
        $this->assign("channel_list", $channel_list);
        //获取父级渠道来源
        $condition['pid'] = array('eq','-2');
        $condition['from_id'] = array('not in','5,6');
        $condition['is_delete'] = 0;
        $pfrom = M("come_from")->where($condition)->select();
        $this->assign('pfrom', $pfrom);
        /**
         * 权限
         */
        $e = new Employee($this->user_info['username']);
        $new_add_auth = $e->check_auth("XinZengQuDaoLaiYuan");    //新增一级权限
        $add_auth = $e->check_auth("TianJiaQuDaoLaiYuan");        //添加二级权限
        $edit_auth = $e->check_auth("BianJiQuDaoLaiYuan");           //修改权限
        $del_auth = $e->check_auth("ShanChuQuDaoLaiYuan");            //删除权限
        $this->assign('new_add_auth',$new_add_auth);
        $this->assign('add_auth',$add_auth);
        $this->assign('edit_auth',$edit_auth);
        $this->assign('del_auth',$del_auth);
        $this->display();
    }

    //获取部门信息
    public function get_channel() {
        $from_id = I('post.from_id/d', 0);
        $channel_info = $this->channel->get_channel($from_id);
        if (empty($channel_info)) {
            $this->helpJsonResult(404, "渠道来源为空");
        }
        $parent = $this->channel->getChannelByFromId($channel_info['pid']);
        $channel_info['parent_name'] = $parent['from_name'];
        $this->helpJsonResult(200, 'info', $channel_info);
    }

    /**
     * 添加渠道来源
     */
    public function add_channel() {
        $type = I('post.type/d');
        if ($type == 1) {
            $pid = I('post.channel_pid/d');
            if (empty($pid)) {
                $this->helpJsonResult(400, "一级来源不能为空");
            }
            $data['pid'] = $pid;
            $from_name = I('post.channel_name');
            if (empty($from_name)) {
                $this->helpJsonResult(400, "二级来源不能为空");
            }
            $channel_name = $this->channel->getChannelDetailByName($from_name);
            if (!empty($channel_name)) {
                $this->helpJsonResult(400, "二级来源已存在");
            }
            $data['from_name'] = $from_name;
            $res = $this->channel->add_channel($data);
            if ($res) {
                $this->helpJsonResult(200, "添加成功");
            }
            $this->helpJsonResult(200, "添加失败");
        } else {
            $parent_channel_name = I('post.parent_channel_name');
            if (empty($parent_channel_name)) {
                $this->helpJsonResult(400, "一级来源不能为空");
            }
            $parent_channel_name_check = $this->channel->getChannelDetailByParentName($parent_channel_name);
            if (!empty($parent_channel_name_check)) {
                $this->helpJsonResult(400, "一级来源已存在");
            }
            $data['from_name'] = $parent_channel_name;
            M()->startTrans();
            $parent_channel_id = $this->channel->add_channel($data);

            $chirden['pid'] = $parent_channel_id;
            $from_name = I('post.channel_name');
            if (empty($from_name)) {
                $this->helpJsonResult(400, "二级来源不能为空");
            }
            $channel_name = $this->channel->getChannelDetailByName($from_name);
            if (!empty($channel_name)) {
                $this->helpJsonResult(400, "二级来源已存在");
            }
            $chirden['from_name'] = $from_name;
            $res = $this->channel->add_channel($chirden);
            if ($parent_channel_id && $res) {
                M()->commit();
                $this->helpJsonResult(200, "添加成功");
            }
            M()->rollback();
            $this->helpJsonResult(200, "添加失败");
        }
    }

    /**
     * 编辑渠道来源
     */
    public function edit_channel() {
        $pid = I('post.pid');
        $from_id = I('post.from_id/d');
        $from_name = I('post.channel_name');
        if ($pid) {
            if ($pid == '-2') {
                $parent_channel_name = I('post.parent_channel_name');
                $parent_channel_name_check = $this->channel->getChannelDetailByParentName($parent_channel_name);
                if (!empty($parent_channel_name_check)) {
                    $this->helpJsonResult(400, "一级来源已存在");
                }
                $parent_data['from_id'] = $from_id;
                $parent_data['from_name'] = $parent_channel_name;
                $res = $this->channel->edit_channel($parent_data);
                if ($res !== false) {
                    $this->helpJsonResult(200, "修改成功");
                }
                $this->helpJsonResult(500, "修改失败");
            } else {
                if (empty($from_name)) {
                    $this->helpJsonResult(400, "二级来源不能为空");
                }
                $channel_name = $this->channel->getChannelDetailByName($from_name);
                if (!empty($channel_name)) {
                    $this->helpJsonResult(400, "二级来源已存在");
                }
                $data['from_id'] = $from_id;
                $data['from_name'] = $from_name;
                $res = $this->channel->edit_channel($data);
                if ($res !== false) {
                    $this->helpJsonResult(200, "修改成功");
                }
                $this->helpJsonResult(500, "修改失败");
            }
        }
    }

    /**
     * 删除渠道来源
     */
    public function del_channel() {
        $from_id = I('post.id/d', 0);
        $channel = new Channel($from_id);
        //检查是否有子渠道
        $children = $channel->get_children_channel();
        if (!empty($children)) {
            $this->helpJsonResult(400, '有子渠道,不能删除');
        }
        $result = $channel->del_channel();
        if (!$result) {
            $this->helpJsonResult(500, '删除失败');
        }
        $this->helpJsonResult(200, '删除成功');
    }

}
